<?php
declare (strict_types = 1);

namespace app\listener;

use app\admin\model\bargain\Bargain;
use app\admin\model\bargain\BargainUser;
use app\admin\model\ErrorLog;
use app\common\service\store\BargainService;

class BargainUserInvalid
{
    /**
     * 事件监听处理
     *
     * @return mixed
     */
    public function handle($event)
    {
        $i = 0;
        while (1) {
            $data = BargainService::bargainUserInvalid($event['bargain_id'],$event['uid']);
            if ($data['code'] == 200) {
                break;
            }
            if ($i > 9) {
                $error[] = $this->errorData(BargainUser::class,$data);
                $error[] = $this->errorData(Bargain::class,$data);
                $errorModel = new ErrorLog();
                $errorModel->saveAll($error);
                return false;
            }
            //延时执行100毫秒
            usleep(100000);
            $i++;
        }
        return true;
    }

    public function errorData($obj,$data)
    {
        $model = new $obj();
        $error['error_sql'] = $model->getLastSql();
        $error['error_msg'] = $data['message'];
        return $error;
    }
}
